<template>
  <div class="ma-content-block">
	  <a-tabs v-model="activeTab" @change="handleTabChange" v-if="false">
	    <a-tab-pane key="zh" title="中文"></a-tab-pane>
	    <a-tab-pane key="en" title="英文"></a-tab-pane>
	    <a-tab-pane key="ja" title="日语"></a-tab-pane>
	    <a-tab-pane key="ko" title="韩语"></a-tab-pane>
	  </a-tabs>  
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="引导标题" field="guidance_title">
            <a-input v-model="searchForm.guidance_title" placeholder="请输入引导标题" />
          </a-form-item>
        </a-col>
      </template>
	  
	  <template #sort_order="{ record }">
	    <a-input-number
	        v-model="record.sort_order"
	        :min="0"
	        style="width: 80px"
	        @change="changeSortOrder(record.guidance_id, record.sort_order)"
	    />
	  </template>

      <!-- Table 自定义渲染 -->
    </sa-table>
	

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/guidance'
import commonApi from '@/api/common'
// 引用定义
const crudRef = ref()
const editRef = ref()
const activeTab = ref('zh') // 默认选中中文
// 搜索表单
const searchForm = ref({
  guidance_title: '',
})


// SaTable 基础配置
const options = reactive({
 api: (params) => api.getPageList({ ...params, ...searchForm.value }),
  pk: 'guidance_id',
 recycleApi: (params) => api.getRecyclePageList({ ...params, ...searchForm.value }), 
  recycleApi: api.getRecyclePageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/parkGuidance/ParkGuidance/save'],
    func: async () => {
     const currentLangCode = searchForm.value.lang_code;
     editRef.value?.open()
     editRef.value?.setFormData({lang_code:currentLangCode})
    },
  },
  edit: {
    show: true,
    auth: ['/parkGuidance/ParkGuidance/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/parkGuidance/ParkGuidance/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/parkGuidance/ParkGuidance/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/parkGuidance/ParkGuidance/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '排序', dataIndex: 'sort_order', width: 180 },
  { title: '引导图标', dataIndex: 'guidance_icon',type:'image', width: 180 },
  { title: '引导标题', dataIndex: 'guidance_title', width: 180 },
  { title: '发布单位', dataIndex: 'issuing_unit', width: 180 },
    { title: '更新时间', dataIndex: 'update_time', width: 180 },
])

// 页面数据初始化
const initPage = async () => {
    searchForm.value.lang_code='zh'
}
const handleTabChange = async (key) => {
   activeTab.value = key
   // 设置语言代码
   searchForm.value.lang_code = key
   // 刷新表格数据
   crudRef.value?.refresh()
}
// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}
const changeSortOrder = async (videoId, sortOrder) => {
  try {
    const response = await api.updateSortOrder({
      id: videoId,
      sort_order: sortOrder
    });
    if (response.code === 200) {
      Message.success('排序修改成功');
      crudRef.value?.refresh();
    } else {
      Message.error('排序修改失败');
    }
  } catch (error) {
    console.error('修改排序出错:', error);
    Message.error('排序修改失败');
  }
}
// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
